﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.world {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.models.world.vos.actors.*;
    import reprise.tweens.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.utils.*;
    import com.robertpenner.easing.*;
    import net.wooga.pets.views.components.controls.*;

    public class DoctorMediator extends AbstractActorMediator {

        public var sessionModel:SessionModel;
        public var pendingModel:PendingModel;
        private var _timestamp:Number;
        private var _coffeeBar:MovieClip;
        private var _coffeeBarTween:SimpleTween;
        private var _textTimer:Timer;
        private var _time:Number;
        private var _timeOld:Number;
        private var _timeNew:Number;

        private static const COFFEE_BAR_WIDTH:int = 380;
        private static const COFFEE_BAR_FILL_WIDTH:int = 267;
        private static const COFFEE_BAR_FILL_OFFSET:int = 50;

        override public function onRegister():void{
            super.onRegister();
            this._textTimer = new Timer(50, 20);
            this._coffeeBarTween = new SimpleTween(500);
            this.view.direction = -1;
            this._coffeeBar = AssetsLibrary.getMovieClip("CoffeeBar");
        }
        override public function onTick(_arg1:Boolean=true, _arg2:Number=0):void{
            var _local3:String;
            this._timestamp = _arg2;
            if (((this.vo.canEat) && ((this.vo.hungryIn < 0)))){
                dispatch(new TickingEvent(TickingEvent.REMOVE_TICKING, this));
                dispatch(new InfoEvent(EventConstants.REMOVE_SPEECHBUBBLE_FROM_LAYER, null));
                this.onTimeTextComplete();
            } else {
                _local3 = (languageModel.getTextByKey("global_hungry_in") + " <br/><font size='54'>[[time_until_ready]]</font>");
                this.vo.showSpeechBubble(SpeechBubbleConstants.TYPE_TEXT_BUBBLE, {text:_local3});
            };
        }
        override protected function onFadeActor(_arg1:InfoEvent):void{
            var _local2:Object;
            if (_arg1.id == id){
                _local2 = _arg1.info;
                if (_local2.enabled){
                    this.view.setEnabled();
                } else {
                    this.view.setDisabled();
                };
                this.view.alpha = _local2.alpha;
                this.view.visible = ((((this.vo.isVisible) && ((_local2.alpha > 0)))) && (this.view.isDisplayObjectInStageBounds()));
            };
        }
        override protected function onMouseOut(_arg1:MouseEvent):void{
            if (this.vo.canEat){
                dispatch(new InfoEvent(EventConstants.CHECK_REMOVE_SPEECHBUBBLE_FROM_LAYER, null, {component:this, vo:this.vo}));
                this.onTimeTextComplete();
                super.onMouseOut(_arg1);
            };
        }
        override protected function onMouseOver(_arg1:MouseEvent):void{
            if (this.vo.canEat){
                if (!this.vo.isHungry){
                    dispatch(new TickingEvent(TickingEvent.ADD_TICKING, this, TimeConstants.SECOND));
                    this.updateCoffeeBar(this.percentage, false);
                };
                super.onMouseOver(_arg1);
            };
        }
        override protected function onClick(_arg1:MouseEvent):void{
            this._timeOld = Math.max(this.vo.hungryIn, 0);
            this._timeNew = ((Math.max(this.vo.hungryIn, 0) + this.sessionModel.doctorMealDuration) - 12);
            this._timeNew = Math.min(this._timeNew, ((this.sessionModel.doctorMealDuration * GameConstants.DOCTOR_MEAL_COUNT) - 10));
            var _local2:InfoEvent = new InfoEvent(EventConstants.FEED, id);
            dispatch(_local2);
            var _local3:InfoEvent = new InfoEvent(EventConstants.DO_NOTHING);
            dispatch(new InfoEvent(EventConstants.ADD_ACTION_TO_AVATAR, this.vo.roomId, _local3));
            var _local4:int = this.pendingModel.getPendingItemCount(GameConstants.PENDING_COFFEE);
            if ((userModel.coffee - _local4) > 0){
                this.updateCoffeeBar(this.percentage);
            };
        }
        override protected function setTextBubble():void{
            var _local3:MovieClip;
            var _local4:TextField;
            var _local5:String;
            var _local6:Point;
            var _local1:ISpeaking = (_vo as ISpeaking);
            var _local2:Object = _local1.speechBubbleData;
            if (_local2.hasOwnProperty("text")){
                _local3 = AssetsLibrary.getMovieClip(SpeechBubbleConstants.TEXT_BUBBLE);
                _local4 = new SpeechBubbleTextfield();
                _local5 = _local2.text;
                if (this._textTimer.running){
                    _local5 = _local5.replace("[[time_until_ready]]", TimeUtils.secondsToHoursMinSec(this._time));
                } else {
                    _local5 = _local5.replace("[[time_until_ready]]", TimeUtils.secondsToHoursMinSec(this.vo.hungryIn));
                };
                _local4.htmlText = _local5;
                _local3.background.width = ((SpeechBubbleConstants.DOC_BUBBLE_MARGIN_LEFT + Math.max(_local4.width, COFFEE_BAR_WIDTH)) + SpeechBubbleConstants.DOC_BUBBLE_MARGIN_RIGHT);
                _local3.background.height = (((SpeechBubbleConstants.DOC_BUBBLE_MARGIN_BOTTOM + _local4.height) + this._coffeeBar.height) + SpeechBubbleConstants.DOC_BUBBLE_MARGIN_TOP);
                _local4.x = (((-(_local3.background.width) / 2) - (_local4.width / 2)) - SpeechBubbleConstants.DOC_BUBBLE_MARGIN_LEFT);
                _local4.y = ((-(_local3.background.height) + SpeechBubbleConstants.DOC_BUBBLE_OFFSET_BOTTOM) + SpeechBubbleConstants.DOC_BUBBLE_MARGIN_TOP);
                this._coffeeBar.x = (((-(_local3.background.width) / 2) - (COFFEE_BAR_WIDTH / 2)) + SpeechBubbleConstants.DOC_BUBBLE_OFFSET_RIGHT);
                this._coffeeBar.y = (SpeechBubbleConstants.DOC_BUBBLE_OFFSET_BOTTOM - this._coffeeBar.height);
                _local3.addChild(this._coffeeBar);
                _local3.addChild(_local4);
                _local6 = this.view.localToGlobal(_local1.speechBubblePosition);
                dispatch(new InfoEvent(EventConstants.ADD_SPEECHBUBBLE_TO_LAYER, null, {mc:_local3, pos:_local6, mouseOverEffect:this.view.getBitmapFilter(), zoom:true, removeTicking:true}));
            };
        }
        private function get percentage():int{
            var _local1:Number = Math.floor(((100 * this.mealCount) / GameConstants.DOCTOR_MEAL_COUNT));
            return (Math.min(100, _local1));
        }
        private function updateCoffeeBar(_arg1:Number, _arg2:Boolean=true):void{
            _arg1 = (_arg1 / 100);
            _arg1 = Numbers.constrain(_arg1, 0, 1);
            l(("percent: " + _arg1));
            var _local3:Number = (((_arg1 * COFFEE_BAR_FILL_WIDTH) - COFFEE_BAR_FILL_WIDTH) + COFFEE_BAR_FILL_OFFSET);
            if (((_arg2) && (!((_local3 == this._coffeeBar.bar.x))))){
                this._coffeeBarTween = new SimpleTween(1000);
                this._coffeeBarTween.addTweenProperty(this._coffeeBar.bar, "x", this._coffeeBar.bar.x, _local3, Quad.easeInOut);
                this._coffeeBarTween.execute();
                this._textTimer.reset();
                this._textTimer.addEventListener(TimerEvent.TIMER, this.onTimeTextTick);
                this._textTimer.addEventListener(TimerEvent.TIMER_COMPLETE, this.onTimeTextComplete);
                this._textTimer.start();
            } else {
                if (!this._coffeeBarTween.isRunning()){
                    this._coffeeBar.bar.x = _local3;
                };
            };
        }
        private function onTimeTextTick(_arg1:TimerEvent):void{
            this._time = ((((this._timeNew - this._timeOld) / 20) * this._textTimer.currentCount) + this._timeOld);
            this.vo.showSpeechBubble(SpeechBubbleConstants.TYPE_TEXT_BUBBLE, this.vo.speechBubbleData);
        }
        private function onTimeTextComplete(_arg1:TimerEvent=null):void{
            this._textTimer.stop();
            this._textTimer.removeEventListener(TimerEvent.TIMER, this.onTimeTextTick);
            this._textTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, this.onTimeTextComplete);
        }
        private function get mealCount():int{
            var _local1:int = (this.vo.hungryAt - tickModel.timestamp);
            var _local2:Number = Math.ceil((_local1 / this.sessionModel.doctorMealDuration));
            l(("count: " + _local2));
            return (_local2);
        }
        private function get view():DoctorView{
            return ((viewComponent as DoctorView));
        }
        private function get vo():DoctorVO{
            return ((_vo as DoctorVO));
        }
        override protected function toggleFacade(_arg1:BuildingEvent=null):void{
            this.updateVisibility();
        }
        private function updateVisibility():void{
            var _local1:Boolean = (this.vo.isVisible) ? !(gameStateModel.isFacadeVisible) : false;
            if (_local1){
                this.view.show();
            } else {
                this.view.hide();
            };
        }

    }
}//package net.wooga.pets.views.world 
